package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApAssociateWords;
import com.heima.search.repository.ApAssociateWordsRepository;
import com.heima.search.service.IAssociateWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AssociateWordsServiceImpl implements IAssociateWordsService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApAssociateWordsRepository apAssociateWordsRepository;

    @Override
    public ResponseResult search(ApArticleSearchDto dto) {
        // 判断关键字是否为空
        if (StringUtils.isEmpty(dto.getSearchWords().trim())) {
            return ResponseResult.okResult(new ArrayList<ApAssociateWords>());
        }
        // 根据用户输入的关键字联想查询
        // 按照创建时间倒序排
        // 取前10条数据
        // like '%'
        List<ApAssociateWords> list = apAssociateWordsRepository.findTop10ByAssociateWordsStartingWithOrderByCreatedTimeDesc(dto.getSearchWords());
        return ResponseResult.okResult(list);
    }
}
